package ace.cmp.util;

import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import org.apache.commons.lang3.StringUtils;

/**
 * @author Caspar 获取本机ip
 */
public final class AceLocalHostIpUtils {

  /**
   * 获取Ip地址,例子：192168010053
   *
   * @return
   */
  public static String getIpCode() {
    String ipCode = StringUtils.EMPTY;
    String ip = getLocalHostAddress();
    String[] ipArray = ip.split("\\.");
    for (int j = 0; j < 4; j++) {
      for (int i = ipArray[j].length(); i < 3; i++) {
        ipCode = ipCode + "0";
      }
      ipCode = ipCode + ipArray[j];
    }

    return ipCode;
  }

  /**
   * <p>
   * 获取第一个本机IP地址。 例子：192.168.10.53
   * </p>
   *
   * @return 本机IP地址 -Djava.net.preferIPv4Stack=TRUE
   */
  public static String getLocalHostAddress() {
    String localHostAddress = StringUtils.EMPTY;
    String ip = "";
    String ipBak = "";
    InetAddress inetAddress = null;
    try {
      inetAddress = InetAddress.getLocalHost();
      ip = inetAddress.getHostAddress();
    } catch (Throwable e1) {
      // e1.printStackTrace();
    }
    if (StringUtils.isNotBlank(ip) && !"127.0.0.1".equals(ip) && ip.indexOf(':') < 0) {
      localHostAddress = ip;
      // log.info("java.net.InetAddress.getLocalHost().getHostAddress()="+ ip);
      return ip;
    }
    Enumeration netInterfaces = null;
    try {
      netInterfaces = NetworkInterface.getNetworkInterfaces();
    } catch (Throwable e) {

    }
    InetAddress iAddress = null;
    try {
      out:
      while (netInterfaces.hasMoreElements()) {
        NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();

        Enumeration<InetAddress> inetAddresses = ni.getInetAddresses();
        while (inetAddresses.hasMoreElements()) {
          iAddress = inetAddresses.nextElement();
          if (!iAddress.isSiteLocalAddress()
              && !iAddress.isLoopbackAddress()
              && iAddress.getHostAddress().indexOf(':') == -1) {
            ip = iAddress.getHostAddress();
            break out;
          } else {
            ip = iAddress.getHostAddress();

            if (!ip.equals("127.0.0.1") && ip.split("\\.").length == 4 && ip.indexOf(':') < 0) {
              ipBak = ip;
            }
            ip = "";
            iAddress = null;
          }
        }
      }
    } catch (Throwable e3) {

    }
    if (!ip.equals("127.0.0.1") && ip.split("\\.").length == 4 && ip.indexOf(':') < 0) {
      localHostAddress = ip;
      return ip;
    }
    try {
      Enumeration<?> e1 = (Enumeration<?>) NetworkInterface.getNetworkInterfaces();
      while (e1.hasMoreElements()) {
        NetworkInterface ni = (NetworkInterface) e1.nextElement();
        if (!ni.getName().equals("eth0")
            && !ni.getName().equals("eth1")
            && !ni.getName().equals("bond0")) {
          // log.info("NetworkInterface.getNetworkInterfaces().nextElement().getName()="+
          // ni.getName());
          continue;
        } else {
          Enumeration<?> e2 = ni.getInetAddresses();
          while (e2.hasMoreElements()) {
            InetAddress ia = (InetAddress) e2.nextElement();
            if (ia instanceof Inet6Address) {
              continue;
            }
            ip = ia.getHostAddress();

            if (!ia.isSiteLocalAddress()
                && !ip.equals("127.0.0.1")
                && ip.split("\\.").length == 4
                && ip.indexOf(':') < 0) {
              localHostAddress = ip;
              return ip;
            }

            if (ni.getName().equals("eth1")
                && !ia.isSiteLocalAddress()
                && !ip.equals("127.0.0.1")
                && ip.split("\\.").length == 4
                && ip.indexOf(':') < 0) {
              ipBak = ip;
              ip = "";
            }
          }
          break;
        }
      }
    } catch (Throwable e) {
      // e.printStackTrace();
    }
    if (!ip.equals("127.0.0.1") && ip.split("\\.").length == 4 && ip.indexOf(':') < 0) {
      localHostAddress = ip;
      return ip;
    }
    if (!ipBak.equals("127.0.0.1") && ipBak.split("\\.").length == 4 && ipBak.indexOf(':') < 0) {
      localHostAddress = ipBak;
      return ipBak;
    }
    localHostAddress = ip;
    return ip;
  }
}
